package cn.jsu.oj.judgment.net.starter;

import cn.jsu.oj.judgment.factory.JudgeFactory;
import cn.jsu.oj.judgment.net.JudgeClient;
import cn.jsu.oj.judgment.net.JudgeWriteClient;

import cn.jsu.oj.judgment.utils.TypeUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;

/**
 * 判题机启动器
 */

@Slf4j
@Component("serviceStarter")
@DependsOn("judgeFactoryConfig")
public class ServiceStarter implements JudgeStarter, CommandLineRunner {


    public static Boolean judgeActiveSign;

    public static Boolean judgeVerifySign;

    public static Boolean judgeWriteActiveSign;

    public static Boolean judgeWriteVerifySign;

    @Autowired
    JudgeFactory judgeStarterFactory;

    @Autowired
    ExecutorService judgeExecutor;


    @Autowired
    JudgeWriteClient judgeWriteClient;
    @Autowired
    JudgeClient judgeClient;


    public ServiceStarter() {
        judgeActiveSign = false;
        judgeVerifySign = false;
        judgeWriteActiveSign = false;
        judgeWriteVerifySign = false;

    }

    @Override
    public void start() {
        TypeUtil.objToMap(judgeStarterFactory.getProduct
                ((short) 0), Short.class, JudgeStarter.class)
                .forEach((key, value) ->
                        judgeExecutor.execute(value::start));
    }

    @Override
    public void run(String... args) throws Exception {
        judgeClient.init();
        judgeWriteClient.init();
        start();
    }
}
